<?php

if (isset($_GET['clear-tweet-stack'])) {
  if (!isset($_GET['email']) || $_GET['email'] == '') {
    die('No email in URL...Fuck you!!!');
  }
  include_once "../config.php";
  require_once "../util.php";
  header ('Content-type: text/html; charset=utf-8');
  connect_db();

  $email = $_GET['email'];
  $sql = "delete from tweet_stack where user_email = '$email'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "SQL:($sql) failed: " . mysql_error();
    return;
  }

  echo mysql_affected_rows() . "行已更新，请等待跳转。。。";
  echo sprintf('<META HTTP-EQUIV="refresh" CONTENT="3; URL=%s">', $_SERVER['HTTP_REFERER']);
  return;
}

// Update user 
if (isset($_POST['email'])) {
  include_once "../config.php";
  require_once "../util.php";
  header ('Content-type: text/html; charset=utf-8');

  connect_db();

  $email = $_POST['email'];
  $enabled = isset($_POST['enabled']) ? 1 : 0;
  $indexed = isset($_POST['indexed']) ? 1 : 0;


  $password = mysql_real_escape_string($_POST['password']);
  $categories = mysql_real_escape_string(str_replace('＃', '#', $_POST['categories']));
  $tags = mysql_real_escape_string(str_replace('＃', '#', $_POST['tags']));
  $poison_tags = mysql_real_escape_string(str_replace('＃', '#', $_POST['poison_tags']));
  $victim_keywords = mysql_real_escape_string(str_replace('＃', '#', $_POST['victim_keywords']));
  $next_action_time = mysql_real_escape_string($_POST['next_action_time']);

  if (isset($_POST['reset-action-time'])) {
    $next_action_time = -1;
  }


  $sql = sprintf('update sina_user set passwd = "%s", categories = "%s", tags = "%s",
victim_keywords="%s", enabled=%d, poison_tags = "%s", indexed = %d, next_action_time = %d
 where email = "%s"',
                 $password, $categories, $tags, $victim_keywords, $enabled, $poison_tags, $indexed, $next_action_time, $email);
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "SQL:($sql) failed: " . mysql_error();
    return;
  }

  echo mysql_affected_rows() . "行已更新，请等待跳转。。。";

  echo sprintf('<META HTTP-EQUIV="refresh" CONTENT="3; URL=%s">', $_SERVER['HTTP_REFERER']);
  return;
}

if (!isset($_GET["email"])) {
  echo "Not found";
}

include_once "../config.php";
require_once "../util.php";

connect_db();

$email = $_GET["email"];
$sql = "select * from sina_user where email = '$email'";
$ret = mysql_query($sql);
if (!$ret) {
  echo "'$sql' failed:<br/>";
  echo mysql_error();
  return;
}

if (mysql_num_rows($ret) != 1) {
  echo "'$sql' returned " . mysql_num_rows($ret) . " rows, WTF?";
  return;
}

$account = mysql_fetch_assoc($ret);

function fetch_tweet_stack($email) {
  $sql = "select count(*) as count from tweet_stack where user_email = '$email'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  $result = mysql_fetch_assoc($ret);
  echo "等待被发送的tweet条数: " . $result['count'];
  echo " -- <a href='edit_user.php?email=$email&&clear-tweet-stack'>删除全部</a>";
  $sql = "select title, href, created_at, id from tweet_stack, tweet_crawled where user_email = '$email' and id = tweet_id order by created_at desc limit 100";
  echo "<br />" .$sql;
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  while ($tweet = mysql_fetch_assoc($ret)) {
    echo "<br />" . sprintf("<a href='%s'>%s</a> -- %s", $tweet['href'], $tweet['title'], $tweet['created_at']) .
      sprintf(" -- <a tweet_id='%s'>X</a>", $tweet['id']);
  }

}

function fetch_victims($email) {
  $sql = "select count(*) as count from victim_crawled where email = '$email'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  $result = mysql_fetch_assoc($ret);
  echo "还可以粉" . $result['count'] . "人";
  $sql = "select victim from victim_crawled where email = '$email' limit 50";
  echo "<br />" .$sql;
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  while ($victim = mysql_fetch_assoc($ret)) {
    echo "<br />" . sprintf("<a href='http://weibo.com/%s'>%s</a>", $victim['victim'], $victim['victim']);

  }

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="css/stats.css" />
    <link rel="stylesheet" type="text/css" href="/lib/css/reset.css">
    <title><?php echo $email?> | 微博账户管理</title>
  </head>
  <body>
    <div id="edit-user">
      <p id="email-p"><?php echo $email?></p>
      <form id="edit-form" action="edit_user.php" method="post">
        <input type="hidden" name="email" value="<?php echo $email?>"></input>
        <label>密码:</label> <input name="password" value="<?php echo $account['passwd']?>" type="text"/>
        <label>categories:</label> <input name="categories" value="<?php echo $account['categories']?>" type="text"/>
        <label>tags:</label> <input name="tags" value="<?php echo $account['tags']?>" type="text"/>
        <label>poison tags:</label> <input name="poison_tags" value="<?php echo $account['poison_tags']?>" type="text"/>
        <label>victim_keywords:</label> <input name="victim_keywords" value="<?php echo $account['victim_keywords']?>" type="text"/>
        <label>enabled:</label><input type="checkbox" name="enabled" <?php if ($account['enabled']) {echo 'checked=""';} ?> />
        <label>indexed:</label><input type="checkbox" name="indexed" <?php if ($account['indexed']) {echo 'checked=""';} ?> />
             next action time: (<?= date('Y-m-d H:m:s', $account['next_action_time'])?>)
        <input type="hidden" name="next_action_time" value="<?= $account['next_action_time']?>" />
        <lable>reset next action time </lable><input type="checkbox" name="reset-action-time" />
       
        <input type="submit" value="更新" />
      </form>
    </div>

    <div id="user-stat">
      <?php fetch_tweet_stack($email); ?>
    </div>

    <div id="to-follow">
      <?php fetch_victims($email); ?>
    </div>
  </body>
</html>
